package com.hluck.materialstudy.ui.activity

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.bumptech.glide.Glide
import com.hluck.materialstudy.R
import com.hluck.materialstudy.data.models.Fruit
import com.hluck.materialstudy.databinding.ActivityFruitBinding

class FruitActivity : AppCompatActivity() {

    private lateinit var mbinding:ActivityFruitBinding

    companion object{
        fun startFruitActivity(context: Context,fruit: Fruit){
            val intent = Intent(context,FruitActivity::class.java)
            intent.putExtra("fruitName",fruit.name)
            intent.putExtra("fruitImageId",fruit.imageId)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mbinding = DataBindingUtil.setContentView(this,R.layout.activity_fruit)
        setSupportActionBar(mbinding.toolbar)
        supportActionBar?.let {
            it.setDisplayHomeAsUpEnabled(true)
        }
        val fruitName = intent.getStringExtra("fruitName") ?: "Mango"
        mbinding.collapsingToolbar.title = fruitName
        Glide.with(this).load(intent.getIntExtra("fruitImageId",R.drawable.mango)).into(mbinding.fruitImg)
        mbinding.fruitText.text = initFruitDetails(fruitName)
    }

    override fun onOptionsItemSelected(item: android.view.MenuItem): Boolean {
        when(item.itemId){
            android.R.id.home -> finish()
        }
        return super.onOptionsItemSelected(item)
    }

    private fun initFruitDetails(fruitName:String) = fruitName.repeat(500)
}